package binaryTree;

/*
 *@description 路径总和
 *@author hkfan
 *@version 1.0
 *@create 2025/6/16 15:36
 */
public class LeetCode_112 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root==null ){
            return false;
        }
        return solution(root,targetSum);
    }

    private boolean solution(TreeNode root, int targetSum) {
        if (root!=null && root.left==null && root.right==null && root.val==targetSum){
            return true;
        }
        if (root==null){
            return false;
        }
        return solution(root.left,targetSum-root.val) || solution(root.right,targetSum-root.val);
    }
}
